<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="币种" prop="currency">
        <el-select
          v-model="queryParams.currency"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in currencyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column
            label="名称"
            prop="name"
            width="200"
            show-overflow-tooltip
            v-if="columns[0].visible"
          />
          <el-table-column
            label="海报"
            prop="posterPath"
            v-if="columns[1].visible"
          >
            <template slot-scope="scope">
              <img
                :src="domainBase + scope.row.posterPath"
                class="tableImageStyle"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="单价"
            prop="price"
            v-if="columns[2].visible"
          />
          <el-table-column
            label="单位"
            prop="priceUnit"
            v-if="columns[3].visible"
          />
          <el-table-column
            label="币种"
            prop="currency"
            v-if="columns[4].visible"
          />
          <el-table-column
            label="简介"
            prop="brief"
            width="200"
            show-overflow-tooltip
            v-if="columns[5].visible"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="200"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleProQuery(scope.row)"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :class="{ showDisabledClas: title == '详情' }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="产品名称" prop="name">
              <el-input
                v-model="form.name"
                :disabled="title == '详情'"
                placeholder="请输入产品名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="产品海报" prop="posterPath">
              <cropper-modal
                v-if="open"
                @getImageUrl="getImageUrl"
                :fixed-number="[1.1, 1]"
                :image-url="form.posterPath"
                :current-handle="title"
              />

              <!--              <image-miner-upload v-if="open"  :value="form.posterPath" @input="getImageUrl" :current-handle="title" :image-width="720" :image-height="672"/>-->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="产品简介" prop="brief">
              <el-input
                v-model="form.brief"
                :disabled="title == '详情'"
                placeholder="请输入产品简介"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投资矿池" prop="currency">
              <el-select
                :disabled="title == '详情'"
                style="width: 100%"
                v-model="form.currency"
                placeholder="请选择"
              >
                <el-option
                  v-for="dict in currencyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合约期限(天)" prop="closePeriod">
              <el-input
                v-model="form.closePeriod"
                placeholder="请输入合约期限"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易结算类型" prop="tradeSettlementType">
              <el-select
                style="width: 100%"
                :disabled="title == '详情'"
                v-model="form.tradeSettlementType"
                placeholder="请选择"
              >
                <el-option
                  v-for="dict in tradeSettlementTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等待期(天)" prop="waitDay">
              <el-input
                v-model="form.waitDay"
                placeholder="请输入等待期"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="封装期(天)" prop="packageDay">
              <el-input
                v-model="form.packageDay"
                placeholder="请输入封装期"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剩余库存" prop="inventory">
              <el-input
                v-model="form.inventory"
                placeholder="请输入剩余库存"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单价" prop="price">
              <el-input
                v-model="form.price"
                placeholder="请输入单价"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价单位" prop="priceUnit">
              <el-input
                v-model="form.priceUnit"
                placeholder="请输入单价单位"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起购倍数" prop="buyMultiples">
              <el-input
                v-model="form.buyMultiples"
                placeholder="请输入起购倍数"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务费率" prop="tariff">
              <el-input
                v-model="form.tariff"
                placeholder="请输入服务费率"
                :disabled="title == '详情'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="产品描述">
              <el-input
                v-model="form.description"
                type="textarea"
                placeholder="请输入产品描述"
                :disabled="title == '详情'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title != '详情'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { productList, productDelete, productAdd, getProduct, productUpdate } from "@/api/biz"
import { nonnegative, isNumber } from '@/utils/validate'
import CropperModal from '@/components/CropperModal'

// import ImageMinerUpload from '@/components/ImageMinerUpload'

export default {
  name: "Product",
  components: {
    CropperModal
  },
  data () {
    const validNonnegative = (rule, value, callback) => {
      if (!nonnegative(value)) {
        callback(new Error("请输入非负整数字"));
      } else {
        callback();
      }
    };
    const validIsNumber = (rule, value, callback) => {
      if (!isNumber(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    return {
      domainBase: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        currency: undefined
      },
      // 币种状态数据字典
      currencyOptions: [
        {
          dictValue: 'BTC',
          dictLabel: 'BTC'
        },
        {
          dictValue: 'CRU',
          dictLabel: 'CRU'
        },
        {
          dictValue: 'XCH',
          dictLabel: 'XCH'
        },
        {
          dictValue: 'BZZ',
          dictLabel: 'BZZ'
        },
        {
          dictValue: 'PHA',
          dictLabel: 'PHA'
        }
      ],
      tradeSettlementTypeOptions: [
        {
          dictValue: '1',
          dictLabel: 'T+1'
        },
        {
          dictValue: '0',
          dictLabel: 'T+0'
        }
      ],
      // 表单参数
      form: {
        id: '',
        name: '',    //产品名称
        brief: '',     //产品简介
        posterPath: '',  //产品海报地址
        currency: '',   //投资币种
        closePeriod: '',   //合约期限
        tradeSettlementType: '',      //交易结算类型
        waitDay: '',    //等待期
        packageDay: '', //封装期
        inventory: '',   //剩余库存
        price: '',   //单价
        priceUnit: '',  //单价单位
        buyMultiples: '',  //起购倍数
        tariff: ''   //服务费率
      },
      // 列信息
      columns: [
        { key: 0, label: `名称`, visible: true },
        { key: 1, label: `海报`, visible: true },
        { key: 2, label: `单价`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `币种`, visible: true },
        { key: 5, label: `简介`, visible: true }
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        posterPath: [
          { required: true, message: "请上传产品海报图片地址", trigger: "blur" }
        ],
        closePeriod: [
          { required: true, message: "合约期限不能为空", trigger: "blur" },
          { required: true, validator: validNonnegative, trigger: "blur" }
        ],
        currency: [
          { required: true, message: "请选择币种", trigger: "change" }
        ],
        tradeSettlementType: [
          { required: true, message: "请选择交易结算类型", trigger: "change" }
        ],
        waitDay: [
          { required: true, message: "等待期不能为空", trigger: "blur" },
          { required: true, validator: validNonnegative, trigger: "blur" }
        ],
        packageDay: [
          { required: true, message: "封装期不能为空", trigger: "blur" },
          { required: true, validator: validNonnegative, trigger: "blur" }
        ],
        inventory: [
          { required: true, message: "封装期不能为空", trigger: "blur" },
          { required: true, validator: validNonnegative, trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" },
          { required: true, validator: validIsNumber, trigger: "blur" }
        ],
        priceUnit: [
          { required: true, message: "单价单位不能为空", trigger: "blur" }
        ],
        buyMultiples: [
          { required: true, message: "起购倍数不能为空", trigger: "blur" },
          { required: true, validator: validNonnegative, trigger: "blur" }
        ],
        tariff: [
          { required: true, message: "服务费率不能为空", trigger: "blur" },
          { required: true, validator: validIsNumber, trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    getImageUrl (message) {
      console.log('message', message)
      this.form.posterPath = ''
      if (message) {
        this.form.posterPath = message
      }
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 查看产品信息*/
    handleProQuery (row) {
      const productId = row.id;
      getProduct(productId).then(response => {
        this.form = response;
        this.open = true;
        this.title = "详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const productId = row.id;
      this.$confirm('是否确认删除产品编号为"' + productId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return productDelete(productId);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      productList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: '',
        name: '',    //产品名称
        brief: '',     //产品简介
        posterPath: '',  //产品海报地址
        currency: '',   //投资币种
        closePeriod: '',   //合约期限
        tradeSettlementType: '',      //交易结算类型
        waitDay: '',    //等待期
        packageDay: '', //封装期
        inventory: '',   //剩余库存
        price: '',   //单价
        priceUnit: '',  //单价单位
        buyMultiples: '',  //起购倍数
        tariff: ''   //服务费率
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form.id = row.id;
      getProduct(this.form.id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            productUpdate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            productAdd(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style  lang="scss">
.tableImageStyle {
  width: 30px;
  vertical-align: middle;
}
.ant-upload-preview {
  background-color: #fff;
  .avatar-uploader {
    .upload-desc {
      position: absolute;
      bottom: 0;
      left: 0;
      color: #ccc;
      font-size: 8px;
      right: 0;
    }
  }
}
::v-deep .avatar-uploader .el-upload--text {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  margin-right: 20px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 22px;
  color: #ccc;
  width: 108px;
  height: 108px;
  line-height: 108px;
  text-align: center;
}
::v-deep.avatar {
  width: 108px;
  height: 108px;
  display: block;
}
.showDisabledClas {
  .el-input.is-disabled .el-input__inner,
  .el-textarea.is-disabled .el-textarea__inner {
    color: #606266;
  }
}
</style>
